package xhwl.insurance.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.File;

import cn.jpush.android.api.JPushInterface;
import xhwl.android.common.util.CommonAppUtil;
import xhwl.android.common.util.SPUtil;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.R;
import xhwl.insurance.util.DataCleanManager;
import xhwl.insurance.util.XHLog;


public class MySetUpActivity extends BaseActivity implements OnClickListener {
    LinearLayout feedback, clean_cache;
    TextView cache;
    Button exitlogin;
    private LinearLayout mZhiNan;
    private RelativeLayout mVersion;
    private TextView mVresionName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_set_up);
    }

    @Override
    public void initViews() {
        initTitleViews();
        String versionName = CommonAppUtil.getVersionName(getApplicationContext());
        clean_cache = (LinearLayout) findViewById(R.id.clean_cache);
        feedback = (LinearLayout) findViewById(R.id.feedback);
        exitlogin = (Button) findViewById(R.id.exitlogin);
        cache = (TextView) findViewById(R.id.cache);
        mZhiNan = (LinearLayout) findViewById(R.id.ll_user_zhinan);
        mVersion = (RelativeLayout) findViewById(R.id.ll_version);
        mVresionName = (TextView) findViewById(R.id.ll_version_name);
        mVresionName.setText(versionName);


    }

    @Override
    protected void setUpTitleView() {
        titleTextView.setText("设置");
    }

    @Override
    public void initListener() {
        clean_cache.setOnClickListener(this);
        feedback.setOnClickListener(this);
        exitlogin.setOnClickListener(this);
        mZhiNan.setOnClickListener(this);
        mVersion.setOnClickListener(this);
    }

    @Override
    protected void processOper() {
        getcache();
    }

    @Override
    public void loadData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.feedback://意见反馈
                startActivity(new Intent(this, FeedBackActivity.class));
                break;
            case R.id.exitlogin:
                cleanLoginData();
                break;
            case R.id.clean_cache:
                cleancachedata();
                break;
            case R.id.ll_user_zhinan://用户指南
                startActivity(new Intent(this, UserGuideActivity.class));
                break;
            case R.id.ll_version:
                startActivity(new Intent(this, VersionActivity.class));
                break;
            default:
                break;
        }
    }

    public void cleancachedata() {
        String cacheDir=mContext.getCacheDir().toString();
        String fileDir=mContext.getFilesDir().toString();
        String dataPath=cacheDir.substring(0,cacheDir.lastIndexOf("/"));
        String dbDir=dataPath+File.separator+"databases";
        try{
//        DataCleanManager.deleteFilesByDirectory(new File(cacheDir+File.separator+"image"));
            //databases refer to DbConstant
//        DataCleanManager.cleanDatabaseByName(this, "Upgrade.db");
//        DataCleanManager.cleanDatabaseByName(this, "sale.db");
            cache.setText("0.0KB");
        }catch(Exception e){
            e.printStackTrace();
            cache.setText("0.0KB");
        }
    }


    public void getcache() {
        try {
            String cacheDir=mContext.getCacheDir().toString();
            String fileDir=mContext.getFilesDir().toString();
            String dataPath=cacheDir.substring(0,cacheDir.lastIndexOf("/"));
            String dbDir=dataPath+File.separator+"databases";

            XHLog.e("dbdir="+dbDir+" cachedir="+cacheDir+" fileDir="+fileDir);
            double cacheSize=DataCleanManager.getFolderSize(new File(cacheDir+File.separator+"image"));
            cacheSize+=DataCleanManager.getFolderSize(new File(dbDir));
            String size = DataCleanManager.getFormatSize(cacheSize);
            if (size.equals("0.0Byte")) {
                size = "0.0KB";
            }
            cache.setText(size);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public void cleanLoginData() {
        SharedPreferences spInfo = SPUtil.getPersonInfoSp();
        SharedPreferences.Editor editor = spInfo.edit();
        editor.putString(SPUtil.SPPersonInfo.MEMBER_ID, "");
        editor.commit();
        JPushInterface.stopPush(getApplicationContext());
        startActivity(new Intent(this, LoginActivityV2.class));
        JPushInterface.stopPush(getApplicationContext());
        finish();
        MainActivity.instance.finish();

    }
}
